package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.PermissionMapper;
import com.atguigu.ssyx.acl.mapper.RolePermissionMapper;
import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.acl.util.PermissionHelper;
import com.atguigu.ssyx.model.acl.Permission;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName: PermissionServiceImpl
 * Package: com.atguigu.ssyx.acl.service.impl
 * Description:
 *
 * @Author: 比特张
 * @Create: 2023/7/13 - 20:34
 * @Version: v1.0
 */
@Slf4j
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public List<Permission> queryAllPermission() {
        //1 查询所有菜单
        List<Permission> allPermissionList = baseMapper.selectList(null);
        //2 转换要求数据格式
        List<Permission> result = PermissionHelper.buildPermission(allPermissionList);
        return result;
    }

    @Override
    public void removeChildById(Long id) {
        //1 创建list集合idList，封装所有要删除菜单id
        List<Long> idList = new ArrayList<>();
        //根据当前菜单Id，获取当前菜单下面所有子菜单，子菜单下面还有子菜单都要获取
        //重点：递归找当前菜单子菜单
        this.getAllPermissionId(id, idList);
        //设置当前菜单id
        idList.add(id);
        baseMapper.deleteBatchIds(idList);
    }

    @Override
    public List<Permission> getPermissionByRoleId(Long roleId) {
        //全部权限列表
        List<Permission> allPermissionList = this.list();
        //根据角色id获取角色权限
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId, roleId);
        List<RolePermission> rolePermissionList = rolePermissionMapper.selectList(wrapper);
        //转换给角色id与角色权限对应Map对象
        List<Long> permissionIdList = rolePermissionList.stream().map(item -> item.getPermissionId())
                .collect(Collectors.toList());
        allPermissionList.forEach(permission -> {
            if (permissionIdList.contains(permission.getId())) {
                permission.setSelect(true);
            } else {
                permission.setSelect(false);
            }
        });

        List<Permission> permissionList = PermissionHelper.buildPermission(allPermissionList);

        return permissionList;
    }

    @Transactional
    @Override
    public void doAssign(Long roleId, Long[] permissionIds) {
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId, roleId);
        rolePermissionMapper.delete(wrapper);

        for (Long permissionId : permissionIds) {
            if (StringUtils.isEmpty(permissionId)) continue;
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermissionMapper.insert(rolePermission);
        }
    }


    /**
     * 重点：递归找当前菜单子菜单
     *
     * @param id     当前菜单id
     * @param idList 最终封装list集合，包含所有菜单id
     */
    private void getAllPermissionId(Long id, List<Long> idList) {
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getPid, id);
        List<Permission> childList = baseMapper.selectList(wrapper);
        //递归查询是否还有子菜单，有继续查询递归查询
        childList.stream().forEach(item -> {
            //封装菜单Id，到idList集合里面
            idList.add(item.getId());
            //递归
            this.getAllPermissionId(item.getId(), idList);
        });
    }
}




